<template>
  <div>
    <!-- Banner -->
    <div class="banner-wrapper" :style="'background:' + currentBannerBgColor">
      <swiper class="banner" v-if="bannerList.length" :options="bannerOptions" ref="banner">
        <!-- slides -->
        <swiper-slide v-for="(item, index) in bannerList" :key="index" @mouseover.native="slideStop('banner')" @mouseout.native="slideStart('banner')">
          <img class="banner-img" :src="item.imgUrl" width="950" height="460" @click="bannerLinkTo(item)">
        </swiper-slide>
        <!-- optional controls -->
        <div class="swiper-pagination" slot="pagination"></div>
      </swiper>
    </div>
    <!-- 白色背景区域 -->
    <div class="main-wrapper">
      <!-- 活动专区 -->
      <h3 class="main-title" id="activeBar"><span>活动专区</span></h3>
      <section class="activity-area">
        <div class="activitites">
          <div class="item" v-for="(active, index) in activities" :key="index" v-if="index < 4">
            <router-link :to="'/activity?activeId=' + active.activityId" target="_blank" class="img" :style="'background-image: url(' + active.coverUrl + ')'"></router-link>
          </div>
        </div>
      </section>
      <!-- 茶品精选 -->
      <h3 class="main-title" id="selectionBar"><span>茶品精选</span></h3>
      <section class="tea-selection">
        <swiper class="selection" :options="selectionOptions" ref="selection" v-if="selectionGoods.length" @mouseover.native="slideStop('selection')" @mouseout.native="slideStart('selection')">
          <swiper-slide v-for="(item, index) in selectionGoods" :key="index">
            <div class="product-item">
              <router-link :to="'/goods/goodsDetail?productId=' + item.productId" target="_blank" class="product-box clearfix">
                <div class="product-img">
                  <img class="img" :src="item.productCover" width="192" height="192">
                </div>
                <div class="product-info">
                  <div class="sell">
                    <span class="discount">{{item.discount ? item.discount : '?'}}折</span>
                    <span class="sell-price">
                      <em>¥</em>
                      <span v-if="item.sellPrice">{{item.sellPrice | priceFormat1}}</span>
                      <span v-else>?</span>
                      <i class="sell-type">/{{item.sellType}}</i>
                    </span>
                    <span class="orignal-price">¥{{item.originalPrice | priceFormat1}}</span>
                  </div>
                  <div class="name">{{item.productName}}</div>
                  <div class="clearfix">
                    <router-link :to="'/store/storeInfo?storeId=' + item.storeId" class="store clearfix">{{item.storeName}}</router-link>
                  </div>
                  <div class="pro-grade-name" v-if="item.proGradeName">{{item.proGradeName}}</div>
                  <div class="discounts clearfix" v-if="item.activityList">
                    <div class="tag-group" v-if="item.activityList.isMore === '1'">
                      <span class="tag" v-for="(tag, index1) in item.activityList.type" :key="index1">{{FavourableActivityTypes.Types[tag]}}</span>
                    </div>
                    <div class="tag-group" v-else>
                      <span class="tag">{{item.activityList.activityName}}</span>
                    </div>
                  </div>
                </div>
              </router-link>
            </div>
          </swiper-slide>
        </swiper>
        <div class="swiper-button-prev prev-btn"><i class="el-icon-arrow-left"></i></div>
        <div class="swiper-button-next next-btn"><i class="el-icon-arrow-right"></i></div>
      </section>
    </div>
    <!-- 楼层区域 -->
    <v-floor
      v-for="(floor, index) in floorList"
      :id="'floor' + index + 'Bar'"
      :key="index"
      :floor="floor">
    </v-floor>
    <!-- 大牌驾到 -->
    <h3 class="main-title"><span>大牌驾到</span></h3>
    <section class="famous-brand" v-if="firstBrand">
      <div class="first-brand" @click="brandLinkTo(firstBrand)">
        <img class="brand-logo" :src="firstBrand.brandUrl" width="100%">
      </div>
      <div class="brand-list row" v-if="brandList.length > 1">
        <div class="brand-item col" v-for="(item, index) in brandList" :key="index" @click="brandLinkTo(item)" :to="'/store/storeInfo?storeId=' + item.id">
          <div class="brand-logo">
            <img :src="item.brandUrl" width="150" height="150">
          </div>
          <p class="brand-name">{{item.brandName}}</p>
        </div>
      </div>
    </section>
    <!-- 茶圈热点 -->
    <h3 class="main-title"><span>茶圈热点</span><router-link class="btn-more" to="/news/newsList">更多&gt;&gt;</router-link></h3>
    <section class="tea-news">
      <ul class="news-list row">
        <li class="col-3" v-for="(news, index) in newsList" :key="index">
          <router-link class="news-item" :to="'/news/newsDetail?id=' + news.id" target="_blank">
            <img :src="news.imgUrl" class="news-img" width="285" height="200">
            <div class="news-info">
              <h4 class="title">{{news.title}}</h4>
              <p class="description">{{news.articleContent}}</p>
            </div>
          </router-link>
        </li>
      </ul>
    </section>
    <vTools/>
    <!-- 快捷导航栏 -->
    <div class="fast-nav-bar" v-show="showFastNav" v-if="fastNav.length">
      <div class="logo"></div>
      <div id="active" :class="{'active': fastNav[0].active}" @click="ScrollTo('#activeBar')">活动<br/>专区</div>
      <div class="list">
        <div id="selection" class="item" :class="{'active': fastNav[1].active}">
          <div class="text" style="width: 35px;" @click="ScrollTo('#selectionBar')">茶品精选</div>
        </div>
        <div class="item"
          v-for="(item, index) in floorList" :key="index"
          :class="{'active': fastNav[index+2].active}"
          @click="ScrollTo(index)">
          <div class="text">{{item.categoryName}}</div>
        </div>
      </div>
      <div id="toTop" @click="ScrollTo('.top')">
        <i class="el-icon-arrow-up"></i>
        <div>返回<br/>顶部</div>
      </div>
    </div>
  </div>
</template>

<script>
import vFloor from '@/components/public/floor'
import vTools from '@/components/layout/tools'
import { getPosition } from '@/util/element'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import { FavourableActivityTypes } from '@/util/enums'
import Jump from 'jump.js'

export default {
  data () {
    return {
      categoryList: this.$store.state.categoryList.length ? this.$store.state.categoryList : JSON.parse(window.localStorage.getItem('categoryList')), // 分类列表
      bannerList: [], // banner内容
      currentBannerBgColor: '', // 当前banner的序号
      bannerOptions: {
        speed: 500,
        autoplay: {
          delay: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false // 用户操作swiper之后，是否禁止autoplay
        },
        loop: true,
        effect: 'fade',
        preventClicks: false,
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        }
      },
      activities: [], // 活动专区内容
      selectionGoods: [], // 茶品精选商品
      selectionOptions: { // 茶品精选swiper配置
        slidesPerView: 5,
        slidesPerGroup: 5,
        spaceBetween: 10,
        speed: 500,
        autoplay: {
          delay: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false // 用户操作swiper之后，是否禁止autoplay
        },
        loop: true,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      },
      FavourableActivityTypes: FavourableActivityTypes,
      firstBrand: {}, // 大牌推荐
      brandList: [], // 品牌列表
      floorList: [], // 楼层列表
      newsList: [], // 茶圈热点
      showFastNav: false,
      fastNav: [], // 快捷导航
      timer: null // 稀释滚动事件的时间
    }
  },
  components: {
    swiper,
    swiperSlide,
    vFloor,
    vTools
  },
  beforeMount () {
    this.init()
  },
  methods: {
    // 初始化
    init () {
      const self = this
      this.bannerOptions.on = {
        slideChange: function () {
          let index = this.realIndex
          self.currentBannerBgColor = self.bannerList[index].backgroundColor
        }
      }
      this.getBanner()
      this.getActivity()
      this.getSelection()
      this.getFamousBrands()
      this.getTeaNews()
      this.getCateFloor().then(() => {
        let len = this.floorList.length
        let active = document.getElementById('activeBar')
        let selection = document.getElementById('selectionBar')
        let arr = [
          {
            id: '#active',
            name: '活动专区',
            top: getPosition(active, 'top'),
            active: false
          },
          {
            id: '#selection',
            name: '茶品精选',
            top: getPosition(selection, 'top'),
            active: false
          }
        ]
        for (let i = 0; i < len; i++) {
          const dom = document.getElementById('floor' + i + 'Bar')
          arr.push({
            id: dom.id.split('floor')[1],
            name: this.floorList[i].categoryName,
            top: getPosition(dom, 'top'),
            active: false
          })
        }
        this.fastNav = arr
      })
      window.onscroll = () => {
        if (typeof this.timer === 'number') {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          this.watchScroll()
        }, 100)
      }
    },
    // 获取轮播图内容
    getBanner () {
      return new Promise(resolve => {
        this.$http.post(this.$api + '/pc/home/Banner', {}).then(res => {
          if (res.responseCode === '0000') {
            this.bannerList = res.data
            return resolve()
          }
        })
      })
    },
    // banner图跳转到
    bannerLinkTo (item) {
      switch (item.linkType) {
        case 1: // 商品
          this.$router.push('/goods/goodsDetail?productId=' + item.linkValue)
          break
        case 2: // 活动
          this.$router.push('/activity?activeId=' + item.linkValue)
          break
        case 4: // 自定义
          window.open(item.linkValue)
          break
        case 5: // 无
          break
      }
    },
    // 获取活动专区内容
    getActivity () {
      this.$http.post(this.$api + '/pc/home/thematicActivity', {}).then(res => {
        if (res.responseCode === '0000') {
          this.activities = res.data
        }
      })
    },
    // 获取茶品精选内容
    getSelection () {
      this.$http.post(this.$api + '/pc/home/selectionGoods', {}).then(res => {
        if (res.responseCode === '0000') {
          this.selectionGoods = res.data
        }
      })
    },
    // 获取分类楼层内容
    getCateFloor () {
      let cateArr = []
      this.categoryList.forEach(item => {
        cateArr.push(item.categoryId)
      })
      return new Promise(resolve => {
        this.$http.post(this.$api + '/pc/home/categoryFloor', {
          categoryId: cateArr.toString()
        }).then(res => {
          if (res.responseCode === '0000') {
            this.floorList = res.data
            return resolve()
          }
        })
      })
    },
    // 获取大牌驾到内容
    getFamousBrands () {
      this.$http.post(this.$api + '/pc/home/recommendedBrand', {}).then(res => {
        if (res.responseCode === '0000') {
          this.brandList = res.data
          this.firstBrand = this.brandList.shift()
        }
      })
    },
    // 大牌驾到链接内容
    brandLinkTo (obj) {
      switch (obj.brandType) {
        case 1: // 活动
          this.$router.push({
            path: '/activity',
            query: {
              activeId: obj.brandValue
            }
          })
          break
        case 2: // 店铺
          this.$router.push({
            path: '/store/storeInfo',
            query: {
              storeId: obj.brandValue
            }
          })
          break
      }
    },
    // 获取茶圈热点内容
    getTeaNews () {
      this.$http.post(this.$api + '/pc/home/queryArticleList', {
        'page': 1,
        'pageSize': 4
      }).then(res => {
        if (res.responseCode === '0000') {
          this.newsList = res.data.list
        }
      })
    },
    // 停止自动轮播
    slideStop (name) {
      this.$refs[name].swiper.autoplay.stop()
    },
    // 开始自动轮播
    slideStart (name) {
      this.$refs[name].swiper.autoplay.start()
    },
    // 监听页面滚动位置
    watchScroll () {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      let len = this.fastNav.length - 1
      if (!len) {
        return false
      }
      if (scrollTop < this.fastNav[0].top - 100) {
        this.showFastNav = false
      } else {
        this.showFastNav = true
      }
      for (; len > -1; len--) {
        if (scrollTop >= this.fastNav[len].top - 100) {
          this.fastNav.forEach(item => {
            item.active = false
          })
          this.fastNav[len].active = true
          break
        }
      }
    },
    // 跳转到
    ScrollTo (target) {
      let tag = target
      if (typeof target === 'number') {
        tag = '#floor' + target + 'Bar'
      }
      Jump(tag)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/basic.scss';
.banner-wrapper {
  transition: all .5s;
  height: 460px;
  .banner {
    left: 125px;
    width: 950px;
    .banner-img {
      cursor: pointer;
    }
  }
}
.activity-area {
  margin: 0 auto;
  width: 1200px;
  .activitites {
    display: flex;
    justify-content: space-between;
    .item {
      flex: 1;
      margin-right: 10px;
      height: 170px;
      transition: all 0.2s ease-in-out;
      &:last-child {
        margin-right: 0;
      }
      &:hover {
        transform: translate3d(0, -2px, 0);
        box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
      }
      .img {
        display: block;
        width: 100%;
        height: 100%;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
      }
    }
  }
}
.tea-selection {
  position: relative;
  margin: 0 auto 40px;
  width: 1200px;
  height: 390px;
  .prev-btn,
  .next-btn {
    margin-top: -28px;
    width: 56px;
    height: 56px;
    line-height: 56px;
    font-size: 24px;
    text-align: center;
    color: #ffffff;
    background: #e5e5e5;
    border-radius: 50%;
  }
  .prev-btn {
    left: -74px;
  }
  .next-btn {
    right: -74px;
  }
}
.main-wrapper {
  width: 100%;
  background: #ffffff;
  overflow: hidden;
}
.famous-brand {
  margin: 0 auto;
  width: 1200px;
  background: #ffffff;
  box-shadow: 0 0 8px rgba(0, 0, 0, .1);
  .first-brand {
    width: 100%;
  }
  .brand-list {
    margin-left: -10px;
    margin-right: -10px;
    padding: 0 40px 30px;
    .brand-item {
      margin-top: 20px;
      padding-right: 10px;
      padding-left: 10px;
      width: 20%;
      text-decoration: none;
      &:hover img {
        box-shadow: 0 2px 8px rgba(0, 0, 0, .2);
        transform: translate(0, -4px);
      }
      .brand-logo {
        padding: 15px 0;
        cursor: pointer;
        img {
          margin: auto;
          transition: all 0.2s;
        }
      }
      .brand-name {
        margin: 0 -10px;
        padding: 8px 0;
        line-height: 1;
        font-size: $fz_20;
        color: $f_main;
        text-align: center;
        background: #ffffff;
      }
    }
  }
}
.tea-news {
  margin: 0 auto 150px;
  width: 1200px;
  .news-list {
    margin-left: -10px;
    margin-right: -10px;
    .col-3 {
      padding-left: 10px;
      padding-right: 10px;
      .news-item {
        display: block;
        text-decoration: none;
        transition: all 0.2s ease-in-out;
        background: #ffffff;
        box-shadow: 0 0 8px rgba(0, 0, 0, .1);
        &:hover {
          transform: translate3d(0, -2px, 0);
          box-shadow: 0 8px 15px rgba(0, 0, 0, .3);
        }
        .news-info {
          padding: 10px 16px;
          .title {
            font-size: $fz_16;
            font-weight: normal;
            color: $f_main;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          .description {
            margin-top: 6px;
            height: 60px;
            font-size: $fz_14;
            line-height: 20px;
            color: $f_normal;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
          }
        }
      }
    }
  }
}
.fast-nav-bar {
  position: fixed;
  z-index: 9999;
  top: 50%;
  right: 73px;
  transform: translateY(-50%);
  width: 60px;
  background: #ffffff;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba($color: #000000, $alpha: 0.3);
  .logo {
    content: '';
    position: absolute;
    top: -30px;
    left: 50%;
    margin-left: -18px;
    width: 36px;
    height: 30px;
    background: url(../../assets/images/fast-nav/logo.png);
  }
  #active {
    padding-top: 10px;
    height: 50px;
    background: url(../../assets/images/fast-nav/bg.png) top center no-repeat;
    font-size: 14px;
    font-weight: bold;
    color: #ffffff;
    text-align: center;
    line-height: 1.2;
    cursor: pointer;
  }
  #toTop {
    text-align: center;
    padding: 3px 0 10px;
    cursor: pointer;
    font-size: 14px;
    .el-icon-arrow-up {
      color: #999999;
    }
  }
  .list {
    padding: 0 10px;
    .item {
      position: relative;
      height: 40px;
      color: #333333;
      text-align: center;
      border-bottom: 1px solid #e5e5e5;
      cursor: pointer;
      .text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 36px;
        font-size: 12px;
      }
      &:hover,
      &.active {
        font-weight: bold;
      }
    }
  }
}
.pro-grade-name {
  margin-top: 6px;
}
</style>
